package com.ehuoyun.ecstore.action;

import java.io.IOException;

import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationError;

import com.ehuoyun.ecstore.biz.AdvertisementMgr;
import com.ehuoyun.ecstore.biz.UserMgr;
import com.ehuoyun.ecstore.model.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * An example of an ActionBean that uses validation annotations on fields instead of
 * on methods.  Logs the user in using a conventional username/password combo and
 * validates the password in the action method.
 *
 * @author Tim Fennell
 */
public class LoginActionBean extends AbstractActionBean {
	
    @Validate(required=true)
    private String username;

    @Validate(required=true)
    private String password;

    private String targetUrl;

	/** The username of the user trying to log in. */
    public void setUsername(String username) { this.username = username; }

    /** The username of the user trying to log in. */
    public String getUsername() { return username; }

    /** The password of the user trying to log in. */
    public void setPassword(String password) { this.password = password; }

    /** The password of the user trying to log in. */
    public String getPassword() { return password; }

    /** The URL the user was trying to access (null if the login page was accessed directly). */
    public String getTargetUrl() { return targetUrl; }

    /** The URL the user was trying to access (null if the login page was accessed directly). */
    public void setTargetUrl(String targetUrl) { this.targetUrl = targetUrl; }

    @SpringBean
	private UserMgr userMgr;
    @SpringBean
    private AdvertisementMgr adMgr;
    
    public Resolution login() {
    	User user = userMgr.getUser(username);
    	adMgr.getAllAdvertisements(null);

        if (user == null) {
            ValidationError error = new LocalizableError("usernameDoesNotExist");
            getContext().getValidationErrors().add("username", error);
            return getContext().getSourcePageResolution();
        }
        else if (!user.getPassword().equals(password)) {
            ValidationError error = new LocalizableError("incorrectPassword");
            getContext().getValidationErrors().add("password", error);
            return getContext().getSourcePageResolution();
        }
        else {
            getContext().setUser(user);
            if (this.targetUrl != null) {
                return new RedirectResolution(this.targetUrl);
            }
            else {
                return new RedirectResolution("/index.jsp");
            }
        }
    }
    
    private void outputLogin() {
        UserService userService = UserServiceFactory.getUserService();
        com.google.appengine.api.users.User user = userService.getCurrentUser();

    	try {
	        if (user != null) {
	        	getContext().getResponse().setContentType("text/plain");
				getContext().getResponse().getWriter().println("Hello, " + user.getNickname());
	        } else {
	        	getContext().getResponse().sendRedirect(userService.createLoginURL(getContext().getRequest().getRequestURI()));
	        }
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
